/**
 *  发送系统消息
 */

const user = require("../models/userDetail")
const message = require("../models/message")

var express = require('express');
var router = express.Router();

const formidable = require('formidable');

router.post('/', (req, res, next) => {
    var form = new formidable.IncomingForm();
    form.parse(req, (err, fields) => {
        if (err) {
            res.json({'state' : false, 'msg' : err});
        }else {
            // console.log(fields.flags);
            // console.log(typeof(fields.flags));
            if(fields.flags.length !== 0){
                user.find({
                    flags: {
                        $all: fields.flags
                    }
                },{
                    UUID: 1
                }, (err, users) => {
                    if (err) {
                        res.json({'state': false, 'msg' : err})
                    } else {
                        if(users.length === 0) {
                            res.json({'state': true, 'msg': '用户不存在！'});
                        }else{
                            let message_list = [];
                            for(var i=0;i<users.length;i++){
                                let tmp = {
                                    type: fields.type,
                                    content: fields.content,
                                    UUID: users[i].UUID,
                                    target: "",
                                    result: "",
                                    status: "unread"
                                };
                                message_list.push(tmp);
                            }
                            message.insertMany(message_list, (err, doc) => {
                                if(err) {
                                    console.log('错误出在这里！')
                                    res.json({'state': false, 'msg': err});
                                }else{
                                    res.json({'state': true, 'msg': '发送成功！'});
                                }
                            })
                        }
                    }
                });
            }else{
                user.find({
                    stuNo: {
                        $in: fields.stuNo
                    }
                },{
                    UUID: 1
                }, (err, users) => {
                    if (err) {
                        res.json({'state': false, 'msg' : err})
                    } else {
                        if(users.length === 0) {
                            res.json({'state': true, 'msg': '用户不存在！'});
                        }else {
                            let message_list = [];
                            for(var i=0;i<users.length;i++){
                                let tmp = {
                                    type: fields.type,
                                    content: fields.content,
                                    UUID: users[i].UUID,
                                    target: "",
                                    result: "",
                                    status: "unread"
                                };
                                message_list.push(tmp);
                            }
                            message.insertMany(message_list, (err, doc) => {
                                if(err) {
                                    console.log('错误出在这里！')
                                    res.json({'state': false, 'msg': err});
                                }else{
                                    res.json({'state': true, 'msg': '发送成功！'});
                                }
                            })
                        }
                    }
                });
            }
        }
    })
});

module.exports = router;